<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.package_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="packageAuditList" @selection-change="handleSelectionChange">
      <el-table-column label="套餐名称" align="center" prop="serviceItemName" />
      <el-table-column label="套餐价格" align="center" prop="serviceItemPrice" />
      <el-table-column label="套餐备注" align="center" prop="serviceItemInfo" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.package_audit_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAuditHistory(scope.row)"
            v-hasPermi="['business:packageAudit:auditHistory']"
          >审核历史</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAuditProcess(scope.row)"
            v-hasPermi="['business:packageAudit:auditProcess']"
          >进度查看</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCancel(scope.row)"
            v-hasPermi="['business:packageAudit:cancel']"
          >撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改套餐审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="套餐id" prop="serviceItemId">
          <el-input v-model="form.serviceItemId" placeholder="请输入套餐id" />
        </el-form-item>
        <el-form-item label="套餐名称" prop="serviceItemName">
          <el-input v-model="form.serviceItemName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="套餐备注" prop="serviceItemInfo">
          <el-input v-model="form.serviceItemInfo" placeholder="请输入套餐备注" />
        </el-form-item>
        <el-form-item label="套餐价格" prop="serviceItemPrice">
          <el-input v-model="form.serviceItemPrice" placeholder="请输入套餐价格" />
        </el-form-item>
        <el-form-item label="流程实例id" prop="instanceId">
          <el-input v-model="form.instanceId" placeholder="请输入流程实例id" />
        </el-form-item>
        <el-form-item label="创建者" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="备注" prop="info">
          <el-input v-model="form.info" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.package_audit_status"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="流程图" :visible.sync="resourceImgOpen" width="1200px">
      <div v-html="resourceImg"></div>
    </el-dialog>

    <!--审核历史-->
    <el-dialog title="审核历史" :visible.sync="auditHistoryOpen" width="1200px" append-to-body>
      <el-table v-loading="loading" :data="auditHistoryList" @selection-change="handleSelectionChange">
        <el-table-column label="任务名称" align="center" prop="taskName" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="耗时" align="center" prop="auditUseTime" />
        <el-table-column label="审批意见" align="center" prop="auditInfo" :show-overflow-tooltip="true" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="historyCancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPackageAudit, getPackageAudit, cancelPackageAudit, addPackageAudit, updatePackageAudit, getAuditProcess,getAuditHistory} from "@/api/business/packageAudit";

export default {
  name: "PackageAudit",
  dicts: ['package_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 套餐审核表格数据
      packageAuditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      resourceImg : null,
      resourceImgOpen : false,
      auditHistoryList:[],
      auditHistoryOpen:false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询套餐审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPackageAudit(this.queryParams).then(response => {
        this.packageAuditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 历史取消按钮
    historyCancel() {
      this.auditHistoryOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceItemId: null,
        serviceItemName: null,
        serviceItemInfo: null,
        serviceItemPrice: null,
        instanceId: null,
        creatorId: null,
        info: null,
        status: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加套餐审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPackageAudit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改套餐审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePackageAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPackageAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 撤销按钮操作 */
    handleCancel(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认撤销套餐审核编号为"' + id + '"的审核？').then(function() {
        return cancelPackageAudit(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销成功");
      }).catch(() => {});
    },
    /** 查看审核进度按钮操作 */
    handleAuditProcess(row) {
      getAuditProcess(row.id).then(response => {
          this.resourceImg = response;
          this.resourceImgOpen = true;
      });
    },
    /** 查看审核进度按钮操作 */
    handleAuditHistory(row) {
      getAuditHistory(row.id).then(response => {
        this.auditHistoryList = response.rows;
        this.auditHistoryOpen = true;
      });
    },
  }
};
</script>
